home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 3.iso / dist / fw_qt3.idb / usr / freeware / Qt / examples / canvas / blendshadow.cpp.z / blendshadow.cpp
Encoding:
C/C++ Source or Header  |  2002-04-08  |  1.2 KB  |  54 lines

  1. #include <qimage.h>
  2. #include <qcolor.h>
  3.  
  4. static inline int blendComponent( int v, int av, int s, int as )
  5. {
  6.     return as*s + av*v -(av*as*s)/255;  
  7. }
  8.     
  9. static inline QRgb blendShade( QRgb v, QRgb s )
  10. {
  11.     //shadow image is already reduced and blurred
  12.     int as = qAlpha(s); 
  13.     int av = qAlpha(v);
  14.     if ( as == 0 || av == 255 )
  15.     return v;
  16.  
  17.     int a = as + av -(as*av)/255;
  18.     
  19.     int r = blendComponent( qRed(v),av, qRed(s), as)/a;
  20.     int g = blendComponent( qGreen(v),av, qGreen(s), as)/a;
  21.     int b = blendComponent( qBlue(v),av, qBlue(s), as)/a;
  22.  
  23.     return qRgba(r,g,b,a);
  24. }
  25.  
  26.  
  27.  
  28. int main( int*, char**)
  29. {
  30.     QImage image( "out.png" );
  31.     image.convertDepth( 32 );
  32.     QImage shade( "outshade.png" );
  33.     shade.convertDepth( 32 );
  34.     int dx = 10;
  35.     int dy = 5;
  36.     
  37.     int w = image.width();
  38.     int h = image.height();
  39.     
  40.     QImage img( w+dx, h+dy, 32 );
  41.     img.setAlphaBuffer( TRUE );
  42.  
  43.     for ( int y = 0; y < h+dy; y++ ) {
  44.     for ( int x = 0; x < w+dx; x++ ) {
  45.         QRgb sh =  (x<dx||y<dy) ? 0 : shade.pixel( x-dx, y-dy );
  46.         QRgb pixel = (x<w&y<h) ? image.pixel( x, y ) : 0;
  47.         img.setPixel( x, y, blendShade( pixel, sh ) ); 
  48.     }
  49.     }
  50.     img.save("blend.png", "PNG" );
  51. }
  52.  
  53.  
  54.